import 'package:distribution_mall_flutter/pages/home/domain/home_entity.dart';
import 'package:distribution_mall_flutter/pages/home/state/app_info_state.dart';
import 'package:distribution_mall_flutter/utils/debug.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:visibility_detector/visibility_detector.dart';

part 'home_provider.g.dart';

@riverpod
class AppInfo extends _$AppInfo {
  @override
  AppInfoState build() {
    return const AppInfoState();
  }

  void setUpdating(bool isUpdating) {
    state = state.copyWith(isUpdating: isUpdating);
  }

  void setCanPop(bool canPop) {
    state = state.copyWith(canPop: canPop);
  }

  void setConfirmText(String confirmText) {
    state = state.copyWith(confirmText: confirmText);
  }

  void setData(
    AppInfoEntity? data,
    bool showVersionUpdate,
    bool showPatchUpdate,
    bool hideCancel,
    String titleVersion,
    String version,
    String curTitleVersion,
  ) {
    state = state.copyWith(
      data: data,
      showVersionUpdate: showVersionUpdate,
      showPatchUpdate: showPatchUpdate,
      hideCancel: hideCancel,
      titleVersion: titleVersion,
      version: version,
      curTitleVersion: curTitleVersion,
    );
  }
}

@riverpod
class HomeIsTab extends _$HomeIsTab {
  @override
  bool build() {
    return true;
  }

  void setVisibilityInfo(VisibilityInfo visibilityInfo) {
    // println('setVisibilityInfo ${state} ${visibilityInfo.visibleFraction * 100}');
    ///目前没有什么特别好的方式实现产品的想法,留到后续处理吧
    ///如果是红色的长条
    if (state) {
      state = visibilityInfo.visibleFraction * 100 > 0;
    } else {
      state = visibilityInfo.visibleFraction * 100 > 0;
    }
  }
}
